package oj_leetcode;

public class AddTwoNumbers {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode returnNode = new ListNode(-1);
        ListNode currNode = returnNode;
        ListNode newNode = new ListNode(-1);
        boolean carry = false;
        while (l1 != null || l2 != null || carry) {
            int l1Value = l1 != null ? l1.val : 0;
            int l2Value = l2 != null ? l2.val : 0;
            int value = l1Value + l2Value;
            if (carry) {
                value += 1;
            }
            carry = value >= 10;
            newNode.val = value % 10;
            currNode.next = newNode;

            currNode = newNode;
            newNode = new ListNode(-1);
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
        }
        return returnNode.next;
    }
}
